﻿Imports System.Drawing
Imports System.Drawing.Image
Imports System.Text
Imports System.ComponentModel
Imports System.Windows.Forms

Public Class RealTimeDataCar


    Private Sub RealTimeDataCar_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'LoadProjectList()
        'FormatDataGridView()
        Me.Timer1.Start()

    End Sub




    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        ' isUpdateFormStatus = True
        Me.Close()

    End Sub

    ''' <summary>
    ''' 为DataGridView添加行号
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub dgvRealDataCar_RowPostPaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles dgvRealDataCar.RowPostPaint
        Dim brush As System.Drawing.Brush = New SolidBrush(dgvRealDataCar.RowHeadersDefaultCellStyle.ForeColor)
        e.Graphics.DrawString((e.RowIndex + 1).ToString(), dgvRealDataCar.DefaultCellStyle.Font, Brush, e.RowBounds.Location.X + 12, e.RowBounds.Y + 5)

    End Sub

    Private Sub TimerRealDataCar_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerRealDataCar.Tick
        LoadProjectList()
        FormatDataGridView()
    End Sub

    ''' <summary>
    ''' 窗体关闭时释放资源
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub RealTimeDataCar_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        Me.TimerRealDataCar.Stop()
        System.GC.Collect()

    End Sub
    Private Sub LoadProjectList()

        Dim carList As IList(Of ProjectInfo.CarProjectInfo)
        carList = CarProjectService.GetAllCarProjectInfo()

        Dim WebList As IList(Of ProjectInfo.CarHDItem)
        WebList = New List(Of ProjectInfo.CarHDItem)
        Try
            For Each car As ProjectInfo.CarProjectInfo In carList
                Dim c As ProjectInfo.CarHDItem = New ProjectInfo.CarHDItem
                Dim webCar As New CarWebServiceAdd.AllService
                Dim CarWebServiceAd As String = KeyValuePairClass.GetValue1("CarWebServiceAdd")
                webCar.Url = String.Format("http://{0}/ccdccservice/services/AllService", CarWebServiceAd)

                Dim realCar As New CarWebServiceAdd.CarRealView
                realCar = webCar.getCarRealData(car.ProId)

                c.ProjectName = car.CarProjectName

                If car.CarProjectType = 1 Then
                    c.ProjectType = "移动车载"
                Else
                    c.ProjectType = "移动小批零"
                End If

                c.AI1 = realCar.ai1
                c.AI2 = realCar.ai2
                c.AI3 = realCar.ai3
                c.AI4 = realCar.ai4
                c.Latitude = ConvertLa(realCar.latitude.ToString)

                c.Latitude_dir = realCar.latitude_dir

                c.Longitude = ConvertLo(realCar.longitude.ToString)
                c.Longitude_dir = realCar.longitude_dir
                c.UpdateTime = realCar.updateTime
                c.Flag = realCar.flag.ToString



                System.Diagnostics.Debug.WriteLine(car.CarProjectName + "运行状态： " + realCar.runStatus.ToString + "         " + "报警状态:" + realCar.alarmStatus.ToString)
                If realCar.runStatus.ToString.Equals("2") Then
                    c.RunStatus = "运行"      
                ElseIf realCar.runStatus.ToString.Equals("1") Then
                    c.RunStatus = "停止"
                Else : realCar.runStatus.ToString.Equals("-1")
                    c.RunStatus = "---"

                End If


                If CInt(realCar.alarmStatus) = 2 Then
                    c.AlarmStatus = "正常"
                ElseIf CInt(realCar.alarmStatus) = 1 Then
                    c.AlarmStatus = "报警"
                Else
                    c.AlarmStatus = "---"
                End If


                If realCar.connectionStatus.ToString.Equals("2") Then
                    c.ConnectionStatus = "连接"
                Else
                    c.ConnectionStatus = "断开"
                End If

                WebList.Add(c)
            Next

        Catch ex As Exception
            Debug.Print("webservice invoke failed: %s\n ", ex.Message())
            Exit Sub


        End Try
        
        Me.dgvRealDataCar.DataSource = WebList

    End Sub


    Private Function ConvertLa(ByVal latitude As String) As String
        Dim count As Integer = latitude.ToString.Length

        If count > 0 Then
            'msg.Show("latitude.IndexOf('.')=" + latitude.IndexOf(".").ToString)

            Dim start As String = Nothing


            If latitude.IndexOf(".") = -1 Then
                start = latitude.Substring(0, latitude.Length) '找到这一点
            Else
                start = latitude.Substring(0, latitude.IndexOf(".")) '找到这一点

            End If

            If start.Equals("-300") Or start.Equals("-300.00") Then
                latitude = "---"
                'Exit For

            Else

                Dim first As String = ""
                If start.Length > 0 Then

                    If start.Length >= 2 Then
                        If start.Length = 2 Then
                            first = start.Substring(0, start.Length - 2) + "00°"
                        Else
                            first = start.Substring(0, start.Length - 2) + "°"

                        End If
                        Dim secoud As String = start.Substring(start.Length - 2)
                        'msg.Show("latitude.IndexOf('.')=" + latitude.IndexOf(".").ToString)
                        'msg.Show("count=" + count.ToString)
                        Dim endString As String = latitude.Substring(latitude.IndexOf("."))
                        latitude = first + secoud + endString + "'N"
                    Else
                        latitude = start + "°00.00'N"
                    End If



                Else
                    latitude = "---"

                End If
            End If


        Else
            latitude = "---"

        End If
        Return latitude
    End Function

    Private Function ConvertLo(ByVal longitude As String) As String
        Dim countL As Integer = longitude.ToString.Length

        If countL > 0 Then
            Dim start As String = Nothing


            If longitude.IndexOf(".") = -1 Then
                start = longitude.Substring(0, longitude.Length) '找到这一点
            Else
                start = longitude.Substring(0, longitude.IndexOf(".")) '找到这一点

            End If


            If start.Equals("-300") Or start.Equals("-300.00") Then
                longitude = "---"

            Else

                Dim first As String = ""
                If start.Length > 0 Then

                    If start.Length >= 2 Then
                        If start.Length = 2 Then
                            first = start.Substring(0, start.Length - 2) + "00°"
                        Else
                            first = start.Substring(0, start.Length - 2) + "°"

                        End If
                        Dim secoud As String = start.Substring(start.Length - 2)

                        Dim endString As String = longitude.Substring(longitude.IndexOf("."))
                        longitude = first + secoud + endString + "'E"
                    Else
                        longitude = start + "°00.00'E"
                    End If



                Else
                    longitude = "---"

                End If

            End If
        Else
            longitude = "---"

        End If
        Return longitude
    End Function

    Public Sub GetDataGridViewISNull()
        Dim count As Integer = Me.dgvRealDataCar.Rows.Count

        If count <= 0 Then
            msg.Show("你查询的范围内没有数据,请重置！")
        End If

    End Sub



    Private Sub dgvRealDataCar_Sorted(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvRealDataCar.Sorted
        For i As Integer = 0 To Me.dgvRealDataCar.Rows.Count - 1
            For j As Integer = 1 To Me.dgvRealDataCar.Columns.Count - 1
                If Me.dgvRealDataCar.Rows(i).Cells(j).Value = "报警" Then
                    Me.dgvRealDataCar.Rows(i).Cells(j).Style.ForeColor = Color.Red
                End If
            Next
        Next
    End Sub

    ''' <summary>
    ''' 格式化关键字
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub dgvRealDataCar_DataBindingComplete(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles dgvRealDataCar.DataBindingComplete
        dgvRealDataCar.Columns("ProjectType").DisplayIndex = 1
        dgvRealDataCar.Columns("ProjectName").DisplayIndex = 2
        dgvRealDataCar.Columns("AlarmStatus").DisplayIndex = 3
        dgvRealDataCar.Columns("RunStatus").DisplayIndex = 4
        dgvRealDataCar.Columns("ConnectStatus").DisplayIndex = 5
        dgvRealDataCar.Columns("Latitude").DisplayIndex = 6
        dgvRealDataCar.Columns("Latitude").Width = 100
        dgvRealDataCar.Columns("Longitude").DisplayIndex = 7
        dgvRealDataCar.Columns("Longitude").Width = 100
        dgvRealDataCar.Columns("AI1").DisplayIndex = 8
        dgvRealDataCar.Columns("AI2").DisplayIndex = 9
        dgvRealDataCar.Columns("AI3").DisplayIndex = 10
        'dgvRealDataCar.Columns("AI3").ValueType = dgvRealDataCar.Columns("RunStatus").ValueType
        dgvRealDataCar.Columns("AI4").DisplayIndex = 11


    End Sub

    Public Sub FormatDataGridView()
        For i As Integer = 0 To dgvRealDataCar.RowCount - 1
            For j As Integer = 0 To dgvRealDataCar.ColumnCount - 1
                Dim AlarmStatus As String = dgvRealDataCar.Rows(i).Cells("AlarmStatus").Value

                If AlarmStatus.Equals("2") Or AlarmStatus.Equals("正常") Then
                    dgvRealDataCar.Rows(i).Cells("AlarmStatus").Value = "正常"
                ElseIf AlarmStatus.Equals("1") Or AlarmStatus.Equals("报警") Then
                    dgvRealDataCar.Rows(i).Cells("AlarmStatus").Value = "报警"
                    Dim cell As DataGridViewCell = Me.dgvRealDataCar.Rows(i).Cells("AlarmStatus") '获取这列
                    cell.Style.ForeColor = Color.Red '设置红色
                Else
                    dgvRealDataCar.Rows(i).Cells("AlarmStatus").Value = "---"
                End If

                Dim a1 As String = dgvRealDataCar.Rows(i).Cells("AI1").Value
                If a1.Equals("-300.00") Or a1.Equals("-300") Then
                    dgvRealDataCar.Rows(i).Cells("AI1").Value = "---"

                End If
                Dim a2 As String = dgvRealDataCar.Rows(i).Cells("AI2").Value
                If a2.Equals("-300.00") Or a2.Equals("-300") Then
                    dgvRealDataCar.Rows(i).Cells("AI2").Value = "---"

                End If

                Dim a3 As String = dgvRealDataCar.Rows(i).Cells("AI3").Value
                If a3.Equals("-300.00") Or a3.Equals("-300") Then
                    dgvRealDataCar.Rows(i).Cells("AI3").Value = "---"

                End If
                Dim a4 As String = dgvRealDataCar.Rows(i).Cells("AI4").Value

                'System.Diagnostics.Debug.WriteLine(dgvRealDataCar.Rows(i).Cells("AI4").ValueType.ToString)
                If a4.Equals("-300") Or a4.Equals("-300.00") Then
                    dgvRealDataCar.Rows(i).Cells("AI4").Value = "---"
                End If


            Next
        Next
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Me.Timer1.Stop()
        LoadProjectList()
        FormatDataGridView()

    End Sub
End Class